import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/match/list/match_page_logic.dart';
import 'package:sports_new/controller/service/config_service.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/base/qkd_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';

import '../../../widgets/base/qkd_text.dart';

class ZqOddsConfigPage extends StatefulWidget {
  const ZqOddsConfigPage({super.key});

  @override
  State<ZqOddsConfigPage> createState() => _ZqOddsConfigPageState();
}

class _ZqOddsConfigPageState extends State<ZqOddsConfigPage> {
  ZqConfig config = Get.find<ConfigService>().zqConfig;
  ConfigService configService = Get.find<ConfigService>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(title: QkdText('足球指数设置')),
      backgroundColor: Colours.scaffoldBg,
      body: VisibilityDetector(
        key: Key('ZqOddsConfigPage'),
        onVisibilityChanged: (info) {
          if (!info.visibleBounds.isEmpty) {
            final matchController =
                Get.find<MatchPageLogic>().getMatchController();
            matchController.updateView();
            update();
          }
        },
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 8),
          child: ListView(children: [
            SizedBox(height: 10),
            QkdWidget.switchCell('指数', config.zqList1 == 1, onTap: () {
              config.zqList1 = (config.zqList1 == 1 ? 0 : 1);
              update();
              configService.update(ConfigType.zqList1, config.zqList1);
              final matchController =
                  Get.find<MatchPageLogic>().getMatchController();
              matchController.updateView();
            }),
            SizedBox(height: 10),
            if (config.zqList1 == 1)
              Styles.card(
                  child: Column(
                children: [
                  QkdWidget.cell(
                      '指数类型',
                      config.zqList7
                          .map((e) => configService.zqOddsType[e])
                          .toList()
                          .join(' | '),
                      isCard: false, onTap: () {
                    Get.toNamed(Routes.zqOddsType);
                  }),
                  QkdWidget.seperateLine(),
                  QkdWidget.cell(
                      '指数公司',
                      configService.oddsCompany!
                          .firstWhere((element) => element.id == config.zqList8)
                          .name!,
                      isCard: false, onTap: () {
                    Get.toNamed(Routes.oddsCompany, arguments: 0);
                  }),
                  QkdWidget.seperateLine(),
                  QkdWidget.cell('指数显示', config.zqList9 == 0 ? '初始' : '即时',
                      isCard: false, onTap: () {
                    Get.toNamed(Routes.oddsShow, arguments: 0);
                  }),
                ],
              ))
          ]),
        ),
      ),
    );
  }
}
